#pragma once
#include "stdafx.h"
#include "BSValue.h"
class BSWriter
{

public:
	virtual ~BSWriter();

	virtual std::string write( const BSValue &root ) = 0;
};


/** \brief Outputs a BSValue in <a HREF="http://www.json.org">JSON</a> format without formatting (not human friendly).
*
* The JSON document is written in a single line. It is not intended for 'human' consumption,
* but may be usefull to support feature such as RPC where bandwith is limited.
* \sa Reader, BSValue
*/
class FastWriter : public BSWriter
{
public:
	FastWriter();
	virtual ~FastWriter(){}

	void enableYAMLCompatibility();

public: // overridden from BSWriter
	virtual std::string write( const BSValue &root );

private:
	void writeValue( const BSValue &value );

	std::string document_;
	bool yamlCompatiblityEnabled_;
};

/** \brief Writes a BSValue in <a HREF="http://www.json.org">JSON</a> format in a human friendly way.
*
* The rules for line break and indent are as follow:
* - Object value:
*     - if empty then print {} without indent and line break
*     - if not empty the print '{', line break & indent, print one value per line
*       and then unindent and line break and print '}'.
* - Array value:
*     - if empty then print [] without indent and line break
*     - if the array contains no object value, empty array or some other value types,
*       and all the values fit on one lines, then print the array on a single line.
*     - otherwise, it the values do not fit on one line, or the array contains
*       object or non empty array, then print one value per line.
*
* If the BSValue have comments then they are outputed according to their #CommentPlacement.
*
* \sa Reader, BSValue, BSValue::setComment()
*/
class StyledWriter: public BSWriter
{
public:
	StyledWriter();
	virtual ~StyledWriter(){}

public: // overridden from BSWriter
	/** \brief Serialize a BSValue in <a HREF="http://www.json.org">JSON</a> format.
	* \param root BSValue to serialize.
	* \return String containing the JSON document that represents the root value.
	*/
	virtual std::string write( const BSValue &root );

private:
	void writeValue( const BSValue &value );
	void writeArrayValue( const BSValue &value );
	bool isMultineArray( const BSValue &value );
	void pushValue( const std::string &value );
	void writeIndent();
	void writeWithIndent( const std::string &value );
	void indent();
	void unindent();
	static std::string normalizeEOL( const std::string &text );

	typedef std::vector<std::string> ChildValues;

	ChildValues childValues_;
	std::string document_;
	std::string indentString_;
	int rightMargin_;
	int indentSize_;
	bool addChildValues_;
};

/** \brief Writes a BSValue in <a HREF="http://www.json.org">JSON</a> format in a human friendly way,
to a stream rather than to a string.
*
* The rules for line break and indent are as follow:
* - Object value:
*     - if empty then print {} without indent and line break
*     - if not empty the print '{', line break & indent, print one value per line
*       and then unindent and line break and print '}'.
* - Array value:
*     - if empty then print [] without indent and line break
*     - if the array contains no object value, empty array or some other value types,
*       and all the values fit on one lines, then print the array on a single line.
*     - otherwise, it the values do not fit on one line, or the array contains
*       object or non empty array, then print one value per line.
*
* If the BSValue have comments then they are outputed according to their #CommentPlacement.
*
* \param indentation Each level will be indented by this amount extra.
* \sa Reader, BSValue, BSValue::setComment()
*/
class  StyledStreamWriter
{
public:
	StyledStreamWriter( std::string indentation="\t" );
	~StyledStreamWriter(){}

public:
	/** \brief Serialize a BSValue in <a HREF="http://www.json.org">JSON</a> format.
	* \param out Stream to write to. (Can be ostringstream, e.g.)
	* \param root BSValue to serialize.
	* \note There is no point in deriving from BSWriter, since write() should not return a value.
	*/
	void write( std::ostream &out, const BSValue &root );

private:
	void writeValue( const BSValue &value );
	void writeArrayValue( const BSValue &value );
	bool isMultineArray( const BSValue &value );
	void pushValue( const std::string &value );
	void writeIndent();
	void writeWithIndent( const std::string &value );
	void indent();
	void unindent();
	static std::string normalizeEOL( const std::string &text );

	typedef std::vector<std::string> ChildValues;

	ChildValues childValues_;
	std::ostream* document_;
	std::string indentString_;
	int rightMargin_;
	std::string indentation_;
	bool addChildValues_;
};
std::string  valueToString( int value );
std::string  valueToString( unsigned int value );
std::string  valueToString( double value );
std::string  valueToString( bool value );
std::string  valueToQuotedString( const char *value );

/// \brief Output using the StyledStreamWriter.
/// \see Json::operator>>()
std::ostream& operator<<( std::ostream&, const BSValue &root );